package com.leyou.item.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.leyou.common.enums.ExceptionEnum;
import com.leyou.common.exceptions.LyException;
import com.leyou.common.utils.BeanHelper;
import com.leyou.common.vo.PageResult;
import com.leyou.item.dto.BrandDTO;
import com.leyou.item.entity.Brand;
import com.leyou.item.mapper.BrandMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
public class BrandService {

    @Autowired
    private BrandMapper brandMapper;

    public PageResult<BrandDTO> pageQuery(Integer page, Integer rows, String sortBy, Boolean desc, String key) {

        //分页查询
        PageHelper.startPage(page, rows);

        //针对某个表生成动态sql，example动态sql生成工具
        Example example = new Example(Brand.class);

        //如果sortBy不为空，则要在基本的查询语句后拼接排序条件
        if (!StringUtils.isBlank(sortBy)) {
            example.setOrderByClause(sortBy + (desc ? " DESC" : " ASC"));//set方法一出相当于在sql语句中写了order by
        }

        //当key不为空要根据key查询
        if (!StringUtils.isBlank(key)) {
            //动态条件拼接工具，可以拼接出任何sql条件
            Example.Criteria criteria = example.createCriteria();

            criteria.andLike("name", "%" + key + "%");
            criteria.orEqualTo("letter", key);

        }


        //按条件对brand表格进行查询,根据动态sql生成工具生成的动态sql查询
        List<Brand> brands = this.brandMapper.selectByExample(example);

        if (CollectionUtils.isEmpty(brands)) {
            throw new LyException(ExceptionEnum.DATA_NOT_FOUND);
        }

        //根据查询条件去统计
        PageInfo<Brand> pageInfo = new PageInfo<>(brands);

        //封装分页返回的数据，三个参数，总条数，总页数，返回的页内容
        return new PageResult<>(pageInfo.getTotal(), pageInfo.getPages(), BeanHelper.copyWithCollection(brands, BrandDTO.class));
    }

    @Transactional
    public void addBrand(BrandDTO brandDTO, List<Long> cids) {

        Brand brand = BeanHelper.copyProperties(brandDTO, Brand.class);

        //保存品牌信息同时，主键回显
        int count = this.brandMapper.insertSelective(brand);

        if (1 != count) {
            throw new LyException(ExceptionEnum.DATA_SAVE_ERROR);
        }

        count = this.brandMapper.insertCategoryBrand(brand.getId(), cids);

        if (count != cids.size()) {
            throw new LyException(ExceptionEnum.DATA_SAVE_ERROR);
        }

    }

    @Transactional
    public void updateBrand(BrandDTO brandDTO, List<Long> cids) {

        //获取品牌数据
        Brand brand = BeanHelper.copyProperties(brandDTO, Brand.class);

        int count = this.brandMapper.updateByPrimaryKeySelective(brand);

        if (1 != count) {
            throw new LyException(ExceptionEnum.DATA_MODIFY_ERROR);
        }

        //TODO 先根据品牌id查询统计对应的分类数量，

        //中间表修改先删除，然后重建关系
        count = this.brandMapper.deleteCategoryBrand(brand.getId());

        //TODO 删除后要比对删除影响的行数和要删除的行数是否一致


        //重建品牌和分类的关系
        count = this.brandMapper.insertCategoryBrand(brand.getId(), cids);

        if (count != cids.size()) {
            throw new LyException(ExceptionEnum.DATA_SAVE_ERROR);
        }
    }

    public BrandDTO queryBrandById(Long brandId) {
        Brand brand = this.brandMapper.selectByPrimaryKey(brandId);
        if (null == brand) {
            throw new LyException(ExceptionEnum.DATA_NOT_FOUND);
        }
        return BeanHelper.copyProperties(brand, BrandDTO.class);
    }

    public List<BrandDTO> queryBrandByCategory(Long cid) {

        List<Brand> brands = this.brandMapper.queryBrandByCategory(cid);

        if (CollectionUtils.isEmpty(brands)) {
            throw new LyException(ExceptionEnum.DATA_NOT_FOUND);
        }
        return BeanHelper.copyWithCollection(brands, BrandDTO.class);
    }


    public List<BrandDTO> queryBrandByIds(List<Long> ids) {

        List<Brand> brands = this.brandMapper.selectByIdList(ids);

        if (CollectionUtils.isEmpty(brands)) {
            throw new LyException(ExceptionEnum.DATA_NOT_FOUND);
        }
        return BeanHelper.copyWithCollection(brands, BrandDTO.class);
    }
}
